import { Component, Input } from '@angular/core';
import { CompanyReportDownloadService } from './companyReportDownload.service';
import { CompanyReportDownloadModel } from './companyReportDownload.model';
import { CompanyModel } from '../../../company.model';
import { LrtmService } from '../../../../../../framework/lrtm/lrtm.service';
import { UtilsService } from '../../../../../../shared/services/utils.service';
import { AppComponent } from '../../../../../../app.component';
import { AnalyticsService } from '../../../../../../shared/services/googleAnalytics.service';
import { GoogleAnalyticsBeacon, ReportAnalytics } from '../../../../../../shared/models/googleAnalytics.model';
import { ModalComponent } from '../../../../../../shared/components/modal/modal.component';

@Component({
    selector: 'app-company-report-download',
    templateUrl: 'companyReportDownload.component.html',
    styleUrls: ['./companyReportDownload.component.scss']
})
export class CompanyReportDownloadComponent {
    @Input() company: CompanyModel;
    @Input() downloadData: CompanyReportDownloadModel;
    @Input() isSummary = false;
    @Input() displayText = 'Generate Report';
    @Input() displayIcon = true;
    @Input() isPrimary = true;
    @Input() analyticsData: ReportAnalytics = undefined;
    @Input() modalSettings: ModalComponent = undefined;

    constructor(
        private rootComponent: AppComponent,
        private companyReportDownloadService: CompanyReportDownloadService,
        private lrtmService: LrtmService,
        private analyticsService: AnalyticsService
    ) { }

    public generateReport(aditionalParams: any = undefined): void {
        // add to most active reports
        this.companyReportDownloadService.addCompanyToMostActiveReports(this.company.id)
            .subscribe(_ => { });

        // set report name
        const reportName = UtilsService.getCompanyReportName(this.company, this.downloadData.reportName, this.isSummary);

        // add isSummary to reportData
        if (this.downloadData.reportData instanceof Object) {
            this.downloadData.reportData.isSummaryReport = this.isSummary;
        }

        //add aditional parameters (custom settings)
        if (aditionalParams) {            
            Object.keys(aditionalParams).forEach(field => {
                this.downloadData.reportData[field] = aditionalParams[field];
            });            
        }

        if (this.analyticsData)
            this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon(this.analyticsData.action, this.analyticsData.category, { CompanyId: this.company.id }));

        // run task
        this.lrtmService
            .runTask(this.rootComponent, reportName, this.downloadData.reportType, this.downloadData.reportData, true)
            .subscribe(report => { 
                //remove parameters
                if(aditionalParams){             
                    Object.keys(aditionalParams).forEach(field => {
                        delete this.downloadData.reportData[field];
                    });            
                }
            });
    }
}
